package supermarket;

import java.util.Scanner;

public class Shopping  {
    static SuperMarket superMarket;

    public static void main(String[] args) {

        init();
        shopping();
    }

    private static void init(){
      superMarket=new SuperMarket();
        superMarket.setMarketName("泰克超市");
        Goods[] g=new Goods[5];
        for (int i=0;i< g.length;i++){
            g[i]=new Goods();
        }
        g[0].setGoodsNumber(1);
        g[0].setGoodsName("手机");
        g[0].setPrice(125.0);

        g[1].setGoodsNumber(2);
        g[1].setGoodsName("平板");
        g[1].setPrice(250.0);

        g[2].setGoodsNumber(3);
        g[2].setGoodsName("鸡蛋");
        g[2].setPrice(1.5);

        g[3].setGoodsNumber(4);
        g[3].setGoodsName("苹果");
        g[3].setPrice(0.5);

        g[4].setGoodsNumber(5);
        g[4].setGoodsName("牛奶");
        g[4].setPrice(2.5);

    superMarket.setGoodslist(g);

    Member[] m=new Member[2];
    for (int x=0;x<m.length;x++){
        m[x]=new Member();
    }
    m[0].setUserName("zhangsan");
    m[0].setPassWorld("123456");

    m[1].setUserName("lisi");
    m[1].setPassWorld("12345678");

    superMarket.setMember(m);


    }

    public static void shopping(){

        System.out.println("------------------------欢迎来到"+superMarket.getMarketName()+"欢迎您----------------------------");
        boolean isSuccess=false;
        while (!isSuccess){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名");
        String UserNAme;
        UserNAme=sc.nextLine();
        System.out.println("请输入密码");
        String PassWOrld;
        PassWOrld=sc.nextLine();
        System.out.println(UserNAme+"  "+PassWOrld);
            Member[] members;
            members=superMarket.getMember();

        for (int i=0;i<members.length;i++) {
            if (UserNAme.equals(members[i].getUserName()) && PassWOrld.equals(members[i].getPassWorld())){
                System.out.println("用户在系统中");
                isSuccess=true;
                //bteak 跳出 当前循环
                break;
                }
            }
        if (!isSuccess){
            System.err.println("用户名或密码错误");
            }
        }

        System.out.println("---------------------欢迎"+superMarket.getMember()+"来"+superMarket.getMarketName()+"购物--------------------------");
        Goods[] goods=superMarket.getGoodslist();
        for (int i=0;i<goods.length;i++){
            System.out.println(goods[i].getGoodsNumber()+"\t"+goods[i].getGoodsName()+goods[i].getPrice()+"元");
        }
        System.out.println("-----------------------------------------------------------------");
        System.out.println("请输入商品编号");
        Scanner scanner=new Scanner(System.in);
        int index=scanner.nextInt();
        System.out.println("请输入商品数量");
        int count=scanner.nextInt();
        //通过商品编号 找到对应商品
        Goods good=goods[index];

        Car car=new Car();
        car.setGoods(good);
        car.setCount(count);

        System.out.println("您购买的商品： "+car.getGoods().getGoodsName()+"单价为： "+car.getGoods().getPrice()+"总价为： "+car.getTotalPrice());

    }
}
